package com.util;

import java.sql.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class DBO {

	private Connection conn;
	private Statement stmt;

	public DBO() {
	}
    private List list;
    private ResultSet rs = null;


    /**
     * ���Ҷ���list
     * @param sql
     * @param clazz
     * @return
     */
    public List<Object> findPageList(String sql,Class clazz){
        this.open();
        try {
            rs = this.executeQuery(sql);
            list=ResultSetHandler.handler(rs, clazz);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.close();
        }
        return list;
        
    }
    /**
     * ���Ҷ���
     * @param sql
     * @param clazz
     * @return
     */
    public Object findObj(String sql,Class clazz){
        this.open();
        Object obj=null;
        try {
            rs = this.executeQuery(sql);
            obj=ResultSetHandler.mapObj(rs, clazz);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.close();
        }
        return obj;
        
    }
 
/**
 * �����޸ģ�ɾ��ĳһ�С�
 * @param sql
 * @return
 */
    public int comUp(String sql) {
        this.open();
        try {
            int i = this.executeUpdate(sql);
            if (i == 1) {
                return 200;
            } else {
                return 300;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 300;
        } finally {
            this.close();
        }
    }

/**
 * ���ҳ��ַ������
 * @param sql
 * @return
 */
    public String getString(String sql) {
        list = new ArrayList();
        this.open();
        try {
            rs = this.executeQuery(sql);
            if (rs.next())
                return rs.getString(1);
            else
                return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            this.close();
        }
    }
/**
 * ���ҳ����������
 * @param sql
 * @return
 */
    public int getCount(String sql) {
        this.open();
        try {
            rs = this.executeQuery(sql);
            rs.next();
            return rs.getInt(1);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            this.close();
        }
    }
/**
 * ���ҳ�С�����
 * @param sql
 * @return
 */
    public float getFloat(String sql) {
        this.open();
        try {
            rs = this.executeQuery(sql);
            rs.next();
            return rs.getInt(1);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            this.close();
        }
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

	/**
	 * �����ݿ�
	 */
	public void open() {
		try {
			conn = JDBCUtilsPlus.getConnection();
			stmt = conn.createStatement();
		} catch (Exception ex) {
			System.err.println("�����ݿ�ʱ����: " + ex.getMessage());
		}
	}

	/**
	 * �ر����ݿ⣬�����ӷ��������ӳ�
	 */
	public void close() {
		JDBCUtilsPlus.colseResource(conn, stmt, rs);
	}

	/**
	 * ִ�в�ѯ
	 */
	public ResultSet executeQuery(String sql) throws SQLException {
		rs = stmt.executeQuery(sql);
		return rs;
	}

	/**
	 * ִ����ɾ��
	 */
	public int executeUpdate(String sql) throws SQLException {
		int ret = 0;
		ret = stmt.executeUpdate(sql);
		return ret;
	}



}
